/**
 * com.dingjian.basedata.org.controller.JobController.java
 */
package com.dingjian.basedata.org.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.common.Pagination;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.basedata.org.model.Job;
import com.dingjian.basedata.org.model.JobLevel;
import com.dingjian.basedata.org.service.JobService;
import com.dingjian.framework.base.controller.ListController;

/**
 * @author FengMy
 *
 * @since 2012-10-6
 */
@Controller
@RequestMapping(value="basedata/job/*")
public class JobListController extends ListController {
	@Autowired
	private JobService jobService;
	
	@Override
	protected CoreEntity createNewEntity() {
		return new Job();
	}
	@Override
	protected String getListView() {
		return "basedata/org/jobList";
	}
	@Override
	protected String getEditView() {
		return "basedata/org/jobEdit";
	}
	@Override
	protected String getListMapper() {
		return "com.dingjian.basedata.org.dao.JobDao.select";
	}
	@Override
	protected BaseService<Job> getService() {
		return jobService;
	}
	
	@RequestMapping(value="jobLevel")
	public void jobLevel(HttpServletResponse response){
		String job = getString("job");
		Pagination<JobLevel> page = new Pagination<JobLevel>();
		if(StringUtils.isEmpty(job)){
			page.setItems(new ArrayList<JobLevel>());
		}else{
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("job", job);
			List<JobLevel> jobLevel = queryExecutor.execQuery("com.dingjian.basedata.org.dao.JobLevelDao.select", param, JobLevel.class);
			page.setItems(jobLevel);
			page.setRecordCount(jobLevel.size());
		}
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	
	@Override
	protected boolean isAllowDelete(CoreEntity entity) {
		Job job = (Job) entity;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("job", job.getId());
		if(queryExecutor.execCount("com.dingjian.basedata.org.dao.PositionDao.select", param )>0){
			setOutputMsg("MSG", "该岗位己被引用,不能删除");
			return false;
		}
		return super.isAllowDelete(entity);
	}
}
